<?php
/**
 * Created by PhpStorm.
 * User: Xwen
 * Date: 2017/1/29
 * Time: 下午 07:02
 */
namespace Home\Controller;
use Think\Controller;

class ConsoleController extends Controller{
    public function index(){
        $d=new UserController();
        $d->login_check(3);
        $userGrade = $_SESSION['userGrade'];
        $manageRegin = $_SESSION['manageRegin'];
        $d=time()-60;

        // $map['isOnLine'] = array('gt',$d);
        $sendInfo=D('send_info');
        if($userGrade=='1'){
            $deviseInfo=$sendInfo->where($map)->select();
        }else if($userGrade=='2'){
            $deviseInfo=$sendInfo->where($map)->where(array('regionNum'=>$manageRegin))->select();
        }else if($userGrade=='3'){
            $deviseInfo=$sendInfo->where($map)->where(array('buildingNum'=>$manageRegin))->select();
        }

        $this->assign('info',$deviseInfo);
        // $this->assign('userName',$_SESSION['loginStatus']);
        $this->display();
    }
    public function publicDevice(){
        $sendInfo=D('send_info');
        $deviseInfo=$sendInfo->where(array('deviceBelong'=>'0'))->select();
        $this->assign('info',$deviseInfo);
        $this->display();
    }
    public function changeServer(){
        $d=new UserController();
        $d->login_check(1);
        $this->display();
    }
    public function detail(){
        $d=new UserController();
        $d->login_check(3);
        $ci=$_GET['ci'];
        $db=$_GET['db'];
        $id=$_GET['id'];
        $_SESSION['id']=$id;
        $_SESSION['ci']=$ci;
        $this->get_forthwith_info();
        if($db){
            $this->assign('dAdd',$db);
        }else{
            $this->assign('dAdd',$ci);
        }
        $send_info = M('send_info');
        $deviseInfo=$send_info->where(array('chipId'=>$ci))->field('deviceType')->find();

        $this->assign('dt',$deviseInfo['devicetype']);
        $this->assign('id',$id);
        $this->assign("deviceId",$ci);
        $this->display();
    }


    public function detialSearch(){
        // dump($_POST);
        $com = I('post.searchTime');
        $ci = I('get.chipid');

        $now_time = strtotime(date("Y-m-d H:i:s"));
        if($com == '1'){
            $search_time = $now_time - (86400*3);
            $time = date("Y-m-d H:i:s",$search_time);

        }else if($com == '2'){
            $search_time = $now_time - (86400*7);
            $time = date("Y-m-d H:i:s",$search_time);

        }else if($com == '3'){
            $search_time = $now_time - (86400*15);
            $time = date("Y-m-d H:i:s",$search_time);

        }else{
            $search_time = $now_time - (86400*30);
            $time = date("Y-m-d H:i:s",$search_time);

        }
        echo $time;
        $mm = M('return_info');
        // $npm['time'] = array('gt',$search_time);
        $npm['chipId'] = $ci;

        $res = $mm->where($npm)->where(strtotime(time)>$search_time)->select();
        echo $mm->getLastSql();
        dump($res);



    }



    public function get_forthwith_info(){
        $deviceId=$_SESSION['id'];
        $chipId=$_SESSION['ci'];
        $return_info=D('return_info');
        $forthwith_info=$return_info->where(array('chipId'=>$chipId))->order('time desc')->limit(15)->select();
        $forthwith_info['time'] = date("Y-m-d h:i:s" ,$forthwith_info['time']);
        $this->assign('info',$forthwith_info);

    }
    public function changeInfo(){
        $d=new UserController();
        $d->login_check(2);
        $deviceId=I('get.chipId');
        $send_info=M('send_info');
        $deviseInfo=$send_info->where(array('chipId'=>$deviceId))->find();
        $this->assign("deviceId",$deviseInfo['id']);
        $this->assign("chipId",$deviseInfo['chipid']);
        $this->assign("regionNum",$deviseInfo['regionnum']);
        $this->assign("deviceType",$deviseInfo['devicetype']);
        $this->assign("deviceBelong",$deviseInfo['devicebelong']);
        $this->assign("buildingNum",$deviseInfo['buildingnum']);
        $this->assign("deviceAdd",$deviseInfo['deviceadd']);
        $this->assign("deviceByname",$deviseInfo['devicebyname']);
        $this->display();

    }
    public function changeDeviceInfo(){
        $d=new UserController();
        $d->login_check(2);
        $info=$_POST;
        $id = $_POST['deviceId'];
        $db = $_POST['deviceByname'];
        $ci = $_POST['chipId'];
        $send_info = M("send_info");
        $send_info -> where (array('id' =>$id)) ->save($info);
        $this->success('修改成功','./detail?id='.$id.'&db='.$db.'&ci='.$ci,3);
    }

    /**
     * 分类搜索
     * 按类型
     * 按楼宇
     */
    public function search(){
        $d=new UserController();
        $d->login_check(2);
        $info = M(send_info);
        $infom = M(user_info);
        $userGrade = $_SESSION['userGrade'];
        $userRegin = $infom ->where(array("name"=>$_SESSION['loginStatus']))->field('manageRegin')->find();
        $_SESSION['userRegin'] = $userRegin['manageregin'];
        if($userGrade == '3'){
            $infoResult = $info->where(array('buildingNum'=>$userRegin['manageregin']))->distinct(true)->field('deviceType')->select();
        }else if($userGrade == '2'){
            $infoResult = $info->where(array('regionNum'=>$userRegin['manageregin']))->distinct(true)->field('deviceType')->select();
        }else{
            $infoResult = $info->distinct(true)->field('deviceType')->select();
        }

        $this->assign("info",$infoResult);
        $this->display();
    }
    public function classifySearch(){
        $search_data = I("post.");
        $search_data['regionNum'] = "aaa";
        $npm0['deviceType'] = $search_data['deviceType'];
        $npm1['chipId'] = $search_data['chipId'];
        $aa['regionNum'] = $_SESSION['userRegin'];

        $sendInfo = M("send_info");
       
            if($search_data['chipId'] == ''){
                if($aa['regionNum'] != "all"){
                    if($search_data['deviceType'] != ''){
                        $res = $sendInfo->where($aa)->where($npm0)->select();
                     
                    }else{
                        $res = $sendInfo->where($aa)->select();
                    } 

                }else{
                    if($search_data['deviceType'] != ''){
                        $res = $sendInfo->where($npm0)->select();
                     
                    }else{
                        $res = $sendInfo->select();
                    } 
                }
                    
                
            }else{
                $res = $sendInfo->where($npm1)->find();
            }
        $this->ajaxReturn(json_encode($res),'json');       
    }
}